package link.tengfei.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import link.tengfei.bean.AdminUser;
import link.tengfei.util.ConnectionFactory;

public class AdminIdDaoImpl implements AdminIdDao {

	public boolean findByPassId(int passid) {
		boolean flag = false;
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		String sql = "select id from admin_user where pass_id =?";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, passid);
			rs = psmt.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				if (id > 0) {
					flag = true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return flag;
	}

	public int addByPassId(int passid) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int result = 0;
		String sql = "insert into admin_user (pass_id,add_date,delete_date,judge,state) values (?,?,?,?,?)";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, passid);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			String adddate = df.format(new Date());
			psmt.setString(2, adddate);
			psmt.setString(3, adddate);
			psmt.setInt(4, 1);
			psmt.setInt(5, 0);
			result = psmt.executeUpdate();
			return result;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return result;
	}

	@Override
	public int findNum() {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		int total = 0;
		String sql = "select count(id) as total from admin_user where pass_id !=1";
		try {
			psmt = conn.prepareStatement(sql);
			rs = psmt.executeQuery();
			while (rs.next()) {
				total = rs.getInt("total");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return total;
	}

	@Override
	public List<AdminUser> findByPage(int page) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		String sql = "select * from admin_user where pass_id!=1 limit ?,10";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, page);
			rs = psmt.executeQuery();
			List<AdminUser> users = new ArrayList<>();
			while (rs.next()) {
				AdminUser user = new AdminUser();
				user.setId(rs.getInt("id"));
				user.setPassid(rs.getInt("pass_id"));
				user.setState(rs.getInt("state"));
				user.setUsername(rs.getString("username"));
				user.setAdddate(rs.getString("add_date"));
				user.setBindingdate(rs.getString("binding_date"));
				users.add(user);
			}
			return users;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return null;
	}

}
